package com.wanmait.schedule.controller;


import com.wanmait.schedule.entity.Apply;
import com.wanmait.schedule.entity.CompanyUser;
import com.wanmait.schedule.entity.Department;
import com.wanmait.schedule.service.ApplyService;
import com.wanmait.schedule.util.Result;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.util.List;

/**
 * <p>
 * 申请表	 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Resource
    private ApplyService applyService;

    /*老板查找需要处理的申请
    * */
    @GetMapping("leader/initApply")
    public Result initApply(HttpServletRequest request){
        CompanyUser companyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        List<Apply> applyList = applyService.initApply(companyId);
        return Result.success(applyList);
    }
    /*同意个人用户加入企业
    * */
    @GetMapping("leader/agree/{id}")
    public Result agree(@PathVariable Integer id){
        applyService.agree(id);
        return Result.success();
    }
    /*拒绝个人用户加入企业
    * */
    @GetMapping("leader/refuse/{id}")
    public Result refuse(@PathVariable Integer id){
        Boolean refuse = applyService.refuse(id);
        if (refuse){
            return Result.success();
        }
        else {
            return Result.error();
        }
    }
    /*老板分配个人用户时查找所有部门
    * */
    @GetMapping("leader/initDepartment")
    public Result initDepartment(HttpServletRequest request){
        CompanyUser initCompanyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = initCompanyUser.getCompanyId();
        List<Department> departments = applyService.initDepartment(companyId);
        return Result.success(departments);
    }
    /*老板同意申请以后进行分配部门
    * */
    @PostMapping("leader/distribution")
    public Result distribution(@RequestBody CompanyUser companyUser,HttpServletRequest request){
        CompanyUser initCompanyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = initCompanyUser.getCompanyId();
        companyUser.setCompanyId(companyId);
        companyUser.setPositionId(3);
        applyService.distribution(companyUser);
        return Result.success();
    }
    /*老板查看各部门人员信息
    页面加载时将该企业下的所有人查找出来
    * */
    @GetMapping("leader/initCompanyUser")
    public Result initCompanyUser(HttpServletRequest request){
        CompanyUser initCompanyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = initCompanyUser.getCompanyId();
        List<CompanyUser>companyUsers = applyService.initCompanyUser(companyId);
        return Result.success(companyUsers);
    }
    /*老板分配成员，不查此成员当前所属的部门
    * */
    @GetMapping("leader/selectDepartment/{id}")
    public Result selectDepartment(@PathVariable Integer id,HttpServletRequest request){
        System.out.println("selectDepartment::"+id);
        CompanyUser initCompanyUser =(CompanyUser) request.getAttribute("companyUser");
        Integer companyId = initCompanyUser.getCompanyId();
        List<Department>departments = applyService.selectDepartment(id,companyId);
        return Result.success(departments);
    }
    /*老板分配成员所属部门
    * */
    @GetMapping("leader/assignment")
    public Result assignment(Integer id,Integer departmentId){
        Boolean flag = applyService.assignment(id,departmentId);
        if (flag){
            return Result.success();
        }
        else {
            return Result.error("修改失败,请重新操作");
        }
    }

//--------------------------------⬆老板功能-------------------------------------------
}
